var errstsr;
/***   Main Function, Called when form is submitted.   ***/
function validateForm(form) {
	var valid = true;
	errstr = 'Please enter ';

	if (!checkEmpty(form)) {
        valid = false;
	}

	if (valid) {
		if (!validateEmail(form)) {
			valid = false;
		}
	}

	if (valid) {
		if (!validateName(form)) {
			valid = false;
		}
	}

	if (!valid) {
		errstr += ".";
		document.getElementById('error').innerHTML = errstr;
		document.getElementById('error').style.display = 'block';
		document.getElementById('error').style.color = '#FF0000';
		document.getElementById('error').style.background = '#FF9999';
	}

	return valid;
}

/***   Checks if form element exists and then if it is empty.   ***/
function checkEmpty(form) {
	var x, x1, x2, x3, valid = true;

	if (document.forms[form]["username"]) {
		x = document.forms[form]["username"].value.replace(/^\s+|\s+$/g, '');
		if (x === "") {
			errstr +=  'a username';
			valid = false;
		}
	}

	if (document.forms[form]["firstname"]) {
		x = document.forms[form]["firstname"].value.replace(/^\s+|\s+$/g, '');
		if (x === "") {
			if (!valid) {
				errstr +=  ' and ';
			}
			errstr +=  'a first name';
			valid = false;
		}
	}

	if (document.forms[form]["lastname"]) {
		x = document.forms[form]["lastname"].value.replace(/^\s+|\s+$/g, '');
		if (x === "") {
			if (!valid) {
				errstr +=  ' and ';
			}
			errstr +=  'a last name';
			valid = false;
		}
	}

	if (document.forms[form]["email"]) {
		x = document.forms[form]["email"].value.replace(/^\s+|\s+$/g, '');
		if (x === "") {
			if (!valid) {
				errstr +=  ' and ';
			}
			errstr +=  'an email';
			valid = false;
		}
	}

	if (document.forms[form]["password"]) {
		x = document.forms[form]["password"].value.replace(/^\s+|\s+$/g, '');
		if (x === "") {
			if (!valid) {
				errstr +=  ' and ';
			}
			errstr +=  'a password';
			valid = false;
		}
	}

	if (document.forms[form]["groupname"]) {
		x = document.forms[form]["groupname"].value.replace(/^\s+|\s+$/g, '');
		if (x === "") {
			if (!valid) {
				errstr +=  ' and ';
			}
			errstr +=  'a group name';
			valid = false;
		}
	}

	if (document.forms[form]["recipients[]"]) {
		x = document.forms[form]["recipients[]"].selectedIndex;
		if (x === -1) {
			if (!valid) {
				errstr +=  ' and ';
			}
			errstr +=  'one or more recipients';
			valid = false;
		}
	}

	if (document.forms[form]["subject"]) {
		x = document.forms[form]["subject"].value.replace(/^\s+|\s+$/g, '');
		if (x === "") {
			if (!valid) {
				errstr +=  ' and ';
			}
			errstr +=  'a subject';
			valid = false;
		}
	}

	if (document.forms[form]["msgtext"]) {
		x = document.forms[form]["msgtext"].value.replace(/^\s+|\s+$/g, '');
		if (x === "") {
			if (!valid) {
				errstr +=  ' and ';
			}
			errstr +=  'a message';
			valid = false;
		}
	}

	if (document.forms[form]["assessment[]"]) {
		x = document.forms[form]["assessment[]"].selectedIndex;
		if (x === 0) {
			if (!valid) {
				errstr +=  ' and ';
			}
			errstr +=  'an assessment code';
			valid = false;
		}
	}

	if (document.forms[form]["state[]"]) {
		x = document.forms[form]["state[]"].selectedIndex;
		if (x === 0) {
			if (!valid) {
				errstr +=  ' and ';
			}
			errstr +=  'an assignment status';
			valid = false;
		}
	}

	if (document.forms[form]["week[]"]) {
		x = document.forms[form]["week[]"].selectedIndex;
		if (x === 0) {
			if (!valid) {
				errstr +=  ' and ';
			}
			errstr +=  'the week';
			valid = false;
		}
	}

	if (document.forms[form]["student1"] && document.forms[form]["student2"] && document.forms[form]["student3"] && document.forms[form]["student4"]) {

		x = document.forms[form]["student1"].value.replace(/^\s+|\s+$/g, '');
		x1 = document.forms[form]["student2"].value.replace(/^\s+|\s+$/g, '');
		x2 = document.forms[form]["student3"].value.replace(/^\s+|\s+$/g, '');
		x3 = document.forms[form]["student4"].value.replace(/^\s+|\s+$/g, '');

		if (x === ""  ||  x1 === ""  ||  x2 === ""  ||  x3 === "") {
			if (!valid) {
				errstr +=  ' and ';
			}
			errstr +=  'all reviews';
			valid = false;
		}
	}
	
	if (document.forms[form]["tutor"]) {
		x = document.forms[form]["tutor"].value.replace(/^\s+|\s+$/g, '');
		if (x === "") {
			if (!valid) {
				errstr +=  ' and ';
			}
			errstr +=  'a tutor';
			valid = false;
		}
	}

	if (document.forms[form]["students[]"]) {
		x = document.forms[form]["students[]"].selectedIndex;
		if (x === 0) {
			if (!valid) {
				errstr +=  ' and ';
			}
			errstr +=  'one or more students';
			valid = false;
		}
	}
	
		
	if (document.forms[form]["location"]) {
		x = document.forms[form]["location"].value.replace(/^\s+|\s+$/g, '');
		if (x === "") {
			if (!valid) {
				errstr +=  ' and ';
			}
			errstr +=  'a location';
			valid = false;
		}
	}
	
	if (document.forms[form]["weekday"]) {
		x = document.forms[form]["weekday"].value.replace(/^\s+|\s+$/g, '');
		if (x === "") {
			if (!valid) {
				errstr +=  ' and ';
			}
			errstr +=  'a day';
			valid = false;
		}
	}
	
	if (document.forms[form]["time"]) {
		x = document.forms[form]["time"].value.replace(/^\s+|\s+$/g, '');
		if (x === "") {
			if (!valid) {
				errstr +=  ' and ';
			}
			errstr +=  'a time';
			valid = false;
		}
	}
	
	return valid;
}

/***   Checks if email address is in correct format   ***/
function validateEmail(form) {
    var email, atpos, dotpos, valid = true;
    if (document.forms[form]["email"]) {
        email = document.forms[form]["email"].value.replace(/^\s+|\s+$/g, '');
        atpos = email.indexOf("@");
        dotpos = email.lastIndexOf(".");

        if (atpos < 1 || dotpos < atpos + 2 || dotpos + 2 >= email.length) {
            errstr += "a valid e-mail address.";
            valid = false;
        }
    }
    return valid;
}

/***   Checks if name has only valid characters   ***/
function validateName(form) {
    var first, last, allowed, i, j, valid = true;
    if (document.forms[form]["firstname"]) {
        first = document.forms[form]["firstname"].value.replace(/^\s+|\s+$/g, '');
        last = document.forms[form]["lastname"].value.replace(/^\s+|\s+$/g, '');
        allowed = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ- '";
        for (i=0; i < first.length; i+=1) { 
            if (allowed.indexOf(first.charAt(i)) === -1) {
                errstr += "a valid first name";
				valid = false;
				break;
            }
        }
		for (j=0; j < last.length; j+=1) { 
            if (allowed.indexOf(last.charAt(j)) === -1) {
				if (!valid) {
					errstr +=  ' and ';
				}
                errstr += "a valid last name";
				valid = false;
				break;
            }
        }
    }
    return valid;
}